package com.fau.cm.action.index;


import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalDailyCallSta;
import com.fau.cm.util.DateUtil;
import com.fau.cm.util.FileNameEncoderUtil;
import com.fau.cm.util.SystemConfig;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 电话统计
 * @author dangjh
 * @datetime 2014年11月28日 下午4:59:22
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallStaAction extends PublicAction implements ModelDriven<MedicalDailyCallSta> {

	private static final long serialVersionUID = -3767253158532129045L;

	private MedicalDailyCallSta call = new MedicalDailyCallSta();
	
	/**
	 * @desc 实例化组电话统计界面
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月28日 下午1:53:44
	 */
	@Action(value = "init_call_group_sta_list", results = {@Result(name = SUCCESS, location = "init_call_group_sta_list.jsp")})
	public String initCallGroupStaList() {
		return SUCCESS;
	}
	
	/**
	 * @desc 加载组电话统计数据
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月28日 下午1:56:16
	 */
	@Action(value = "call_group_sta_view")
	public String callGroupStaView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		call.setHospitalId(user.getEnterpriseId());
		if(SystemConfig.CM_SYSTEM_TYPE.equals(user.getDeptType())) {
			call.setGroupId(user.getGroup().getId());
		}
		list = this.baseAllService.getStatisticsService().findGroupCallsStatistics(call);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 导出组电话统计信息excel
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月28日 下午11:47:22
	 */
	@Action(value = "exports_group_call_info")
	public String exportsGroupCalls() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			call.setHospitalId(user.getEnterpriseId());
			if(SystemConfig.CM_SYSTEM_TYPE.equals(user.getDeptType())) {
				call.setGroupId(user.getGroup().getId());
			}
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"组电话统计信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsService().exportGroupCallStatistics(call);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 实例化中心电话统计界面
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月28日 下午1:53:44
	 */
	@Action(value = "init_call_center_sta_list", results = {@Result(name = SUCCESS, location = "init_call_center_sta_list.jsp")})
	public String initCallCenterStaList() {
		return SUCCESS;
	}
	
	/**
	 * @desc 加载中心电话统计数据
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月28日 下午1:56:16
	 */
	@Action(value = "call_center_sta_view")
	public String callCenterStaView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(SystemConfig.CM_SYSTEM_TYPE.equals(user.getDeptType())) {
			call.setCenterId(user.getGroup().getPlatform().getId());
		}
		list = this.baseAllService.getStatisticsService().findPlatformCallsStatistics(call, user);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 导出中心电话统计信息excel
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月28日 下午11:47:22
	 */
	@Action(value = "exports_center_call_info")
	public String exportsCenterCalls() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			call.setHospitalId(user.getEnterpriseId());
			if(SystemConfig.CM_SYSTEM_TYPE.equals(user.getDeptType())) {
				call.setCenterId(user.getGroup().getPlatform().getId());
			}
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心电话统计信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsService().exportPlatformCallStatistics(call);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalDailyCallSta getModel() {
		return call;
	}

}
